<?php

namespace app\admin\controller;


use think\facade\Db;
use think\facade\View;
use common\Nodes;
use common\Tree;

use app\admin\model\AuthGroup as AuthGroupModel;


class AuthGroup extends Common
{

    /**
     * 用户组列表
     * @return array
     * @author admin
     * @date 2019-08-12
     */
    public function index()
    {
        if (request()->isPost()) {
            $list = Db::name("auth_group")->select();
            return ['code' => 0, 'msg' => '获取成功！', 'data' => $list];
        }
        return view();
    }

    /**
     * 添加分组
     * @return array
     * @author admin
     * @date 2019-08-12
     */
    public function add()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $data['create_time'] = time();
                $result = Db::name("auth_group")->insert($data);
                if ($result) {
                    return ['code' => 0, 'msg' => '用户组添加成功！'];
                }
                return ['code' => 3001, 'msg' => '操作失败！'];
            } else {
                return View::fetch('edit');
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 修改分组
     * @return array
     * @author admin
     * @date 2019-08-12
     */
    public function edit()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $result = Db::name("auth_group")->where("group_id", $data['group_id'])->update($data);
                if ($result) {
                    return ['code' => 0, 'msg' => '操作成功！'];
                }
                return ['code' => 3001, 'msg' => '操作失败！'];
            } else {
                $id = input('id');
                $info = Db::name("auth_group")->where("group_id", $id)->find();
                $this->assign('info', $info);
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 删除管理员分组
     * @return array
     * @author admin
     * @date 2019-08-12
     */

    public function doDelete()
    {
        try {
            $id = input("id", 0, "intval");
            if (!$id) {
                return $result = ['code' => 3000, 'msg' => '非法请求！'];
            }
            $result = Db::name("auth_group")->where("group_id", $id)->delete();
            if ($result) {
                return $result = ['code' => 0, 'msg' => '删除成功！'];
            }
            return $result = ['code' => 3000, 'msg' => '操作失败！'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 分组配置规则列表
     * @return array
     * @author admin
     * @date 2019-08-12
     */
    public function access()
    {
        if (request()->isPost()) {
			
			$id=input('id');//用户组ID
			if(!$id){
				return ['status' => ['code' => 10002, 'message' => '非法参数！']];
			}
			$doAction=input('doAction');
			if($doAction=='saveAccess'){
				return self::saveAccess();
			}
            $arr = Nodes::lists();
            $Tree = new Tree();
            $arr = $Tree->menu($arr);
            $rules = Db::name('auth_group')->where('group_id',$id)->value('rules');
            $ruleArr = explode(',', $rules);
            $arr = $Tree->getTree($arr, 0, $ruleArr);
            return ['status' => ['code' => 200, 'message' => '获取成功！'], 'data' => $arr];
        } else {
            $info = Db::name("auth_group")->where("group_id", input("get.id"))->find();
            $this->assign('info', $info);
            return View::fetch();
        }
    }


    /**
     * 保存角色权限
     * @return array
     * @author admin
     * @date 2019-08-12
     */

    private function saveAccess()
    {
        try {
            $rules = input('post.rules/a');
            if (empty($rules)) {
                return ['msg' => '请选择权限！', 'code' => 1];
            }
            $data = input('post.');
            $data['rules'] = implode(',', $rules);
			$authGroup=AuthGroupModel::find($data['id']);
			if(!$authGroup){
				 return ['msg' => '未找到角色！', 'code' => 1];
			}
			$result=$authGroup->save($data);
            if ($result) {
                return ['msg' => '操作成功！', 'code' => 0];
            } else {
                return ['msg' => '保存错误！', 'code' => 1];
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }
	
	
	
}